<?php

class ForgotpasswordController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body	

    	$request = $this->getRequest();
    	if($request->isPost())
    	{
    		$emailaddress = $request->getPost('emailaddress');
    		
    		$criteria = new Criteria();
    		$criteria->add(UserPeer::USERNAME,$emailaddress,Criteria::EQUAL);
    		$oUserCnt = UserPeer::doCount($criteria);
    		$oUserDetails  = UserPeer::doSelectOne($criteria);
    		
    		if($oUserCnt > 0)
    		{
    					
    			
	    			$string ="";
	    			$digits="";
	    			$letters="";
	    			
		    		 $x = range('A','Z');
		    		 $y = range('0','9');
		    		 $z = range('a','z');
				  for ($i=0;$i<=2;$i++) 
				  {
					   @$string .= $x[rand(0,count($x))];
					   @@$digits .= $y[rand(0,9)];
					   @$letters.= $z[rand(0,count($z))];
				  }
		    		 $password = $string.$digits.$letters;
		    		
		    		 $userId = $oUserDetails->getId();
		    		 $oUser	=	UserPeer::retrieveByPK($userId);
		    		 $oUser->setPassword(md5($password))
		    		 	   ->save();
						
						//sending email to merchant email id and as well as logged in user mail id
						$mailDetails = array(
							"name"			    => 	$oUser->getScreenName(),
							"password"			=>  $password,
			      			"email"				=>  $request->getPost('emailaddress'),
			      			"to"				=>	$request->getPost('emailaddress'),
							"subject"			=>	"Your password Information",

						);
			
			$oMail = new Default_Service_Mail();
			$oMail->sendMail("forgotpassword", $mailDetails);
		     
		     $this->view->info =  "An email has been sent to ".$emailaddress;
		     
		    
    	    }
    		else 
    		{
    			 $this->view->info =  "Enter valid Email address";
    		}
    	}
		
    }
    
    

}

